package org.rency.crawler.common.helper;

import java.util.regex.Pattern;

/**
 * 描述: URL帮助类
 * 最近修改时间:2017/8/30 16:17<br>
 *
 * @author CaiyuRen
 * @version V1.0
 * @since 2017/8/30 16:17
 */
public class UrlHelper {

  public static final Pattern DEPTH_PATTERN = Pattern.compile("/");

  private UrlHelper(){}

  /**
   * 正则表达式验证URL
   * </br>Created on 2016年2月24日
   */
  public static boolean validator(String url) {
    return url.matches("^(http|https|ftp)\\://([a-zA-Z0-9\\.\\-]+(\\:[a-zA-"
        + "Z0-9\\.&%\\$\\-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{"
        + "2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}"
        + "[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|"
        + "[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-"
        + "4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0"
        + "-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.[a-zA-Z]{2,4})(\\:[0-9]+)?(/"
        + "[^/][a-zA-Z0-9\\.\\,\\?\\'\\\\/\\+&%\\$\\=~_\\-@]*)*$");
  }

  /**
   * 获取URL深度
   */
  public static int getDepth(String url) {
    return DEPTH_PATTERN.matcher(url).groupCount();
  }

}